package com.yanqu.road.dao.impl.activity.jiuzhou;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jiuzhou.JiuZhouUserBaseData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class JiuZhouUserBaseDataDaoImpl extends TempDataObjectDao<JiuZhouUserBaseData> {
    public static String getJiuZhouUserBaseDataSql = "select * from `t_u_jiu_zhou_user_base_data` where `activityId`=? and `userId`=?";
    public JiuZhouUserBaseData getJiuZhouUserBaseData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getJiuZhouUserBaseDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateJiuZhouUserBaseDataSql = "update `t_u_jiu_zhou_user_base_data` set hasSignUp=?,guessMap=?,getGuessRewardMap=?,lastWorshipTime=?,zoneType=?,lastRoundId=?,supportRewardTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(JiuZhouUserBaseData jiuZhouUserBaseData) {
        jiuZhouUserBaseData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouUserBaseData.getHasSignUp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jiuZhouUserBaseData.getGuessMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jiuZhouUserBaseData.getGetGuessRewardMap())));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouUserBaseData.getLastWorshipTime()));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouUserBaseData.getZoneType()));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouUserBaseData.getLastRoundId()));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouUserBaseData.getSupportRewardTime()));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouUserBaseData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouUserBaseData.getUserId()));
        if (getDb().executeNoneQuery(updateJiuZhouUserBaseDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertJiuZhouUserBaseDataSql = "insert into `t_u_jiu_zhou_user_base_data`(activityId,userId,hasSignUp,guessMap,getGuessRewardMap,lastWorshipTime,zoneType,lastRoundId,supportRewardTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(JiuZhouUserBaseData jiuZhouUserBaseData) {
        jiuZhouUserBaseData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouUserBaseData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouUserBaseData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouUserBaseData.getHasSignUp()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jiuZhouUserBaseData.getGuessMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jiuZhouUserBaseData.getGetGuessRewardMap())));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouUserBaseData.getLastWorshipTime()));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouUserBaseData.getZoneType()));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouUserBaseData.getLastRoundId()));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouUserBaseData.getSupportRewardTime()));
        if (getDb().executeNoneQuery(insertJiuZhouUserBaseDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected JiuZhouUserBaseData parseData(ResultSet rs) throws SQLException {
        JiuZhouUserBaseData jiuZhouUserBaseData = new JiuZhouUserBaseData();
        jiuZhouUserBaseData.setActivityId(rs.getInt("activityId"));
        jiuZhouUserBaseData.setUserId(rs.getLong("userId"));
        jiuZhouUserBaseData.setHasSignUp(rs.getInt("hasSignUp"));
        jiuZhouUserBaseData.setGuessMap(JSON.parseObject(rs.getString("guessMap"), new TypeReference<ConcurrentHashMap<Integer, Map<Integer, Long>>>() {}));
        jiuZhouUserBaseData.setGetGuessRewardMap(JSON.parseObject(rs.getString("getGuessRewardMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Integer>>() {}));
        jiuZhouUserBaseData.setLastWorshipTime(rs.getLong("lastWorshipTime"));
        jiuZhouUserBaseData.setZoneType(rs.getInt("zoneType"));
        jiuZhouUserBaseData.setLastRoundId(rs.getInt("lastRoundId"));
        jiuZhouUserBaseData.setSupportRewardTime(rs.getLong("supportRewardTime"));
        return jiuZhouUserBaseData;
    }
}
